package com.hfjyz.system.user.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.hfjyz.base.action.GridBaseAction;
import com.hfjyz.base.bean.SearchCondition;
import com.hfjyz.base.constant.BaseConstant;
import com.hfjyz.base.exception.BaseException;
import com.hfjyz.base.util.MD5;
import com.hfjyz.system.depart.bean.DepartBean;
import com.hfjyz.system.user.bean.UserBean;
import com.hfjyz.system.user.bean.UserSearchCondition;
import com.hfjyz.system.user.service.UserService;

public class UserAction extends GridBaseAction {

    /**
     * serialVersionUID
     */
    private static final long serialVersionUID = -2897488633343376638L;

    private UserService service = null;
    private UserBean user = null;
    private String selectIds = null;
    private String oldPassword = null;
    private String newPassword = null;

    public UserBean getUser() {
        return user;
    }

    public void setUser(UserBean user) {
        this.user = user;
    }

    public String getSelectIds() {
        return selectIds;
    }

    public void setSelectIds(String selectIds) {
        this.selectIds = selectIds;
    }

    public String getOldPassword() {
        return oldPassword;
    }

    public void setOldPassword(String oldPassword) {
        this.oldPassword = oldPassword;
    }

    public String getPassword() {
        return newPassword;
    }

    public void setPassword(String newPassword) {
        this.newPassword = newPassword;
    }

    public UserService getService() {
        return service;
    }

    public void setService(UserService service) {
        this.service = service;
    }

    @Override
    protected List<UserBean> getBeanList() throws BaseException {
        return service.findUser(getSearchCondition());
    }

    @Override
    protected long getTotalCount() throws BaseException {
        return service.getUserCount(getSearchCondition());
    }

    public String save() {
        map = new HashMap<String, Object>();
        try {
            if (user.getPassword() == null || "".equals(user.getPassword())) {
                String defaultPassowrd = MD5.GetMD5Code(getText(BaseConstant.RESET_PASSWORD));
                user.setPassword(defaultPassowrd);
            }
            service.saveUser(user);
            return SUCCESS;
        } catch (BaseException e) {
            map.put("errorMsg", e.getMessage());
            return ERROR;
        }
    }

    public String delete() {
        map = new HashMap<String, Object>();
        try {
            if (selectIds == null) {
                throw new BaseException("请选择需要删除的用户。");
            }
            String[] idArray = selectIds.split(",");
            if (idArray.length == 0) {
                throw new BaseException("请选择需要删除的用户。");
            }
            List<Integer> idList = new ArrayList<Integer>();
            for (String id : idArray) {
                idList.add(Integer.valueOf(id));
            }
            service.deleteUser(idList);
            return SUCCESS;
        } catch (BaseException e) {
            map.put("errorMsg", e.getMessage());
            return ERROR;
        }
    }

    public String updateInit() throws BaseException {
        map = new HashMap<String, Object>();
        try {
            if (selectIds == null) {
                throw new BaseException("请选择需要删除的用户。");
            }

            Integer id = Integer.valueOf(selectIds);
            user = service.updateInitUser(id);
            map.put("user", user);

            List<DepartBean> departList = service.listDepart();
            map.put("departInfo", departList);
            return SUCCESS;
        } catch (BaseException e) {
            map.put("errorMsg", e.getMessage());
            return ERROR;
        }
    }

    public String passwordChange() {
        map = new HashMap<String, Object>();
        HttpSession session = ServletActionContext.getRequest().getSession();
        UserBean user = (UserBean) session.getAttribute(BaseConstant.SESSION_USER_INFO_KEY);
        try {
            service.passwordChange(user.getId(), oldPassword, newPassword);
        } catch (BaseException e) {
            map.put("errorMsg", e.getMessage());
            return ERROR;
        }

        return SUCCESS;
    }

    public String userSearch() {
        try {
            user = service.findUser(user.getUserId());
            return SUCCESS;
        } catch (BaseException e) {
            map.put("errorMsg", e.getMessage());
            return ERROR;
        }
    }

    public String passwordReset() {
        String encryptPassword = MD5.GetMD5Code(getText(BaseConstant.RESET_PASSWORD));
        try {
            service.resetPassword(user.getUserId(), encryptPassword);
            return SUCCESS;
        } catch (BaseException e) {
            map.put("errorMsg", e.getMessage());
            return ERROR;
        }
    }

    public String pwdChangeInit() {
        return SUCCESS;
    }

    public String pwdResetInit() {
        return SUCCESS;
    }

    public String execute() {
        return SUCCESS;
    }
    
    public SearchCondition getSearchCondition() {
        SearchCondition cond = new UserSearchCondition();
        cond.setPageNum(pageNum);
        cond.setRowCountPerPage(rowCountPerPage);
        cond.setSortColumn(sortColumn);
        cond.setSortDirection(sortDirection);

        return cond;
    }
}
